<?php

namespace app\common\controller;

use think\Controller as Controller;
use app\common\logic\UserLogic as UserLogic;
use app\common\logic\Oauth2 as Oauth2;

class Apibase extends Controller {

    protected $userLogic;
    protected $auth;
    protected $resDatas;
    protected $resError;
    protected $oauth2;

    public function _initialize() {
        $this->userLogic = new UserLogic();
        $this->oauth2 = new Oauth2();
        $this->resDatas = [
            'msg' => '获取数据成功!',
            'code' => 1,
            'status' => 'success',
            'datas' => []
        ];
        $this->resError = [
            'msg' => '数据获取失败',
            'code' => 0,
            'status' => 'error',
            'datas' => [],
        ];
        $this->allowapp();
        if(request()->method()=='OPTIONS'){
            $this->resError['msg'] = '预加载';
            return $this->resError;
        }

        if(request()->method()!='OPTIONS'){
            $accesstoken = $this->oauth2->verifyAccessToken();
            if ($accesstoken > 0) {
                $result = $this->userLogic->getOne($accesstoken);
                $company = db('company')->where('id',getOrgId($result['id'],'org_id'))->value('status');
                if((($result['status'] == 6) && getOrgId($result['id'],'group_id') ==3) || (($company == 4) && getOrgId($result['id'],'group_id') ==3 && (getOrgId($result['id'],'role_id') == 1))){
                    $this->errorDoRedirectUriCallback('', 'invalid_client', NULL, NULL, NULL);
                }
                if($result) {
                    $this->auth = $result;
                } else {
                    $this->resError['msg'] = '登录失效,请重新登录!';
                    $this->resError['code'] = 2;
                    return $this->resError;
                }
            } else {
                $this->errorDoRedirectUriCallback('', 'invalid_client', NULL, NULL, NULL);
            }
        }
    }

    protected function allowapp() {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods:GET, POST');
        header('Access-Control-Allow-Headers: Authorization');
    }

    private function errorDoRedirectUriCallback($redirect_uri, $error, $error_description = NULL, $error_uri = NULL, $state = NULL) {
        $result["query"]["error"] = $error;

        if ($state)
            $result["query"]["state"] = $state;

        if ($this->getVariable('display_error') && $error_description)
            $result["query"]["error_description"] = $error_description;

        if ($this->getVariable('display_error') && $error_uri)
            $result["query"]["error_uri"] = $error_uri;

        $this->doRedirectUriCallback($redirect_uri, $result);
    }

    private function doRedirectUriCallback($redirect_uri, $params) {
        header("HTTP/1.1 " . $redirect_uri . "404 Not Found " . $params['query']['error']);
//        header("Location: " . $this->buildUri($redirect_uri, $params));
        exit;
    }

    private function getVariable($name, $default = NULL) {
        return isset($this->conf[$name]) ? $this->conf[$name] : $default;
    }

    /**做成public以便逻辑成调用
     * @return bool
     */
    public function getApiAuth(){
        return $this->auth ? $this->auth : false;
    }

}
